package org.smarthings.coap;

import org.eclipse.californium.core.CoapHandler;
import org.eclipse.californium.core.CoapResponse;
import org.eclipse.californium.core.coap.MediaTypeRegistry;
import org.smarthings.Callback;
import org.smarthings.Message;

public class CoapHandlerImpl implements CoapHandler {
	private String uri;
	private Callback callback;
	private boolean firstCall = true;

	public CoapHandlerImpl(String uri, Callback callback) {
		this.uri = uri;
		this.callback = callback;
	}

	public void onLoad(CoapResponse response) {
		if (firstCall) {
			firstCall = false;
			return;
		}

		String contentType = MediaTypeRegistry.toString(response.getOptions().getContentFormat());
		callback.message(Message.createReciever(response.getPayload(), CoapFactory.PROTOCOL, uri, contentType,
				CoapUtil.getHttpCode(response)));
	}

	public void onError() {
		// do nothing
	}
}
